import React from 'react';
import { View, Image } from 'react-native';
import { Lt, Color, Label, Dialog, Jpp, Jx, Images, Navigator, Input, Button, Nav, Storage, cSys } from "../_utils";
import { Net, NET_NR, NET_NT, NET_RI, NET_ND } from "../_net"
import { toMainWithToken } from './ApplyAutoLogin';
import { cApply } from './_apply';


const c_placeholders = ["手机号", "密码"];
export class ApplyLogin extends React.PureComponent {
    constructor(props){
        super(props);
        const { account } = this.props.navigation.state.params;
        this.inputs = [account || "", ""];
        this.onClick = this.onClick.bind(this);
        
    }
    static navigationOptions = {
        headerTitle : null,
        headerStyle: {
            borderBottomWidth:0,
            elevation:0,
        },
    }

    onClick(){
        for(let k = 0 ; k < this.inputs.length; ++k){
            let str = Jx.checkInterger(this.inputs[k]);
            if(str){
                Dialog.msg1(c_placeholders[k] + str);
                return;
            }
        }
        cApply.setVisitor(false);
        Net.request("loginUser", {phone:this.inputs[0], validate:this.inputs[1]}, {NET_RI, NET_NT, NET_ND}).then(uData=>{
            console.warn(Jx.stringify(uData));
            Dialog.release();
            if(this.inputs[0] == 13798529601){
                if(this.inputs[1] != 131072){
                    Dialog.msg1(c_placeholders[0] + "或" + c_placeholders[1] + "错误");
                    return;
                }
            }
            if(!toMainWithToken(uData.data, this.inputs[0])){
                Dialog.msg1(c_placeholders[0] + "或" + c_placeholders[1] + "错误");
            }
        }).catch(err=>{
            Dialog.msg1(c_placeholders[0] + "或" + c_placeholders[1] + "错误");
        })
    }
    render(){
        let marh = 25;
        return <View style = {{flex:1}}>
            <Nav.StatusBar theme = {false}/>
            <Label style = {{fontSize:Lt.c20, margin:marh}}>手机登录</Label>
            <Input.NumberLine style = {{
                marginHorizontal:marh,
                marginVertical:Lt.margin,
            }}
                placeholder={"输入" + c_placeholders[0]} 
                defaultValue = {this.inputs[0]}
                onChangeText = {t=>this.inputs[0]=t}
            />
            <Input.NumberLine style = {{
                marginHorizontal:marh,
                marginVertical:Lt.margin,
            }}
                placeholder={"输入" + c_placeholders[1]} 
                defaultValue = {this.inputs[1]}
                onChangeText = {t=>this.inputs[1]=t}
            />
            <Button.FlexPure  title = {"登录"} onPress = {this.onClick}/>
        </View>
    }
}